new Vue({
    el: '#container',
    delimiters: ['${', '}'],
    data() {
        return {
            details: {},
            api_id: '',
            api_path: '',
            api_params: '',
            api_name: '',
            api_method: '',
            api_desc: '',
            group_id: 'defaultgroup',
            groupList: [],
            paramsList: [],
            group_name:''
        }
    },
    mounted() {
        this.findGroupList();
    },
    methods: {
        clickMethods(id) {
            $.get(`/admin/show?api_id=${id}`).then((res) => {
                if (res.success) {
                    this.api_id = res.result.api_id;
                    this.api_path = res.result.api_path;
                    this.paramsList = JSON.parse(res.result.api_params);
                    this.api_name = res.result.api_name;
                    this.api_method = res.result.api_method;
                    this.api_desc = res.result.api_desc;
                    this.group_id = res.result.group_id + '';
                }
            }).catch(function (error) {
                console.log(error);
            });
        },
        findGroupList() {
            $.get(`/admin/group?system_id=${sessionStorage.getItem('group')}`).then((json) => {
                if (json.success) {
                    this.groupList = json.result;
                }
            }).catch(function (error) {
                console.log(error);
            });
        },
        //添加接口
        submitMethods() {
            let _this = this;
            console.table({
                system_id: sessionStorage.getItem('group'),
                api_path: _this.api_path,
                api_params: JSON.stringify(_this.paramsList),
                api_name: _this.api_name,
                api_method: _this.api_method,
                api_desc: _this.api_desc,
                group_id: _this.group_id,
            })



            $.ajax({
                url: `/admin/add`,
                type: "POST",
                data: {
                    system_id: sessionStorage.getItem('group'),
                    api_path: _this.api_path,
                    api_params: JSON.stringify(_this.paramsList),
                    api_name: _this.api_name,
                    api_method: _this.api_method,
                    api_desc: _this.api_desc,
                    group_id: _this.group_id,
                },
                dataType: "json",
            }).then((res) => {
                if (res.success) {
                  location.reload();
                }
            })
        },
        //修改接口
        updateMethods() {
            $.ajax({
                url: `/admin/update?api_id=${this.api_id}`,
                type: "POST",
                data: {
                    system_id: sessionStorage.getItem('group'),
                    api_id: this.api_id,
                    api_path: this.api_path,
                    api_params: JSON.stringify(this.paramsList),
                    api_name: this.api_name,
                    api_method: this.api_method,
                    api_desc: this.api_desc,
                    group_id: this.group_id,
                },
                dataType: "json",
            }).then((res) => {
                if (res.success) {
                    location.reload();
                }
            })
        },
        //删除接口
        deleteMethods() {
            let flag = confirm('确定删除该接口?');
            if (flag == true) {
                $.get(`/admin/delete?api_id=${this.api_id}`).then((res) => {
               
                    if (res.success) {
                        location.reload();
                    }
                }).catch(function (error) {
                    console.log(error);
                });
            }
        },
        //添加参数
        addApiParams() {
            this.paramsList.push({
                id: Math.random().toString(16).substr(2),
                param_key: '',
                param_dec: '',
                param_null: '是',
                param_value: ''
            })
        },
        //删除参数
        deleteApiParams(index, item) {
            console.log(index)
            this.paramsList.splice(index, 1)
        },
        addTypeApi(){
            this.system_id = '';
            this.group_name = '';
            $('#myDialog').modal().show();
        },
        submitGroupMethods(){
           // addgroup
            $.post('/admin/addgroup',{
                system_id:sessionStorage.getItem('group'),
                group_name:this.group_name
            }).then((res)=>{
               if(res.success){
                $('#myDialog').modal().hide()
               // location.reload();
               this.findGroupList();
               }
            })
        },
        //删除
        deleteGroupItem(id,index){
            let flag = confirm('确定删除该接口分组?删除后，该下面的接口将不再显示，谨慎操作！！');
            if (flag == true) {
            $.post('/admin/delgroup',{
                group_id:id
            }).then((res)=>{
               if(res.success){
                this.groupList.splice(index,1)
               }
            })
        }
        }
    }
})